package com.zzzzzz.config;

import java.util.Properties;

import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;
import org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter;

@Configuration
public class ExceptionHandlerConfig {

	/**
	 * Required for security annotations to work in this servlet
	 */
	
	@Bean
	public DefaultAdvisorAutoProxyCreator configDefaultAdvisorAutoProxyCreator() {
		return new DefaultAdvisorAutoProxyCreator();
	}
	
	@Bean
	public AuthorizationAttributeSourceAdvisor configAuthorizationAttributeSourceAdvisor() {
		return new AuthorizationAttributeSourceAdvisor();
	}
	
	/**
	 * Exception Handler
	 */

	@Bean
	public AnnotationMethodHandlerAdapter configAnnotationMethodHandlerAdapter() {
		return new AnnotationMethodHandlerAdapter();
	}
	
	@Bean
	public SimpleMappingExceptionResolver configSimpleMappingExceptionResolver() {
		SimpleMappingExceptionResolver simpleMappingExceptionResolver = new SimpleMappingExceptionResolver();
		Properties exceptionMappings = new Properties();
		exceptionMappings.setProperty(AuthorizationException.class.getName(), "exception/permission_denied");
		simpleMappingExceptionResolver.setExceptionMappings(exceptionMappings);
		simpleMappingExceptionResolver.setDefaultErrorView("error");
		return simpleMappingExceptionResolver;
	}

}
